package com.voja.salonschedule.customers;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.voja.salonschedule.R;
import com.voja.salonschedule.data.CustomerBalance.BalanceItem;
import com.voja.salonschedule.data.Event;

public class CreditAdapter extends ArrayAdapter<BalanceItem> {
	
	private Context context;
	private int     resourceId;
	private ArrayList<BalanceItem> items;
	
	public CreditAdapter(Context context, int resourceId, ArrayList<BalanceItem> items) {
        super(context, resourceId, items);

        this.context = context;
        this.resourceId = resourceId;
        this.items = items;
    }
	
	@Override
	public BalanceItem getItem(int pos) {
		return items.get(pos);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BalanceItem item = getItem(position);
		
		if (convertView == null) {
			convertView = ((Activity)context).getLayoutInflater()
                    		.inflate(resourceId, parent, false);
		}
		
		TextView txtDate = (TextView)convertView.findViewById(R.id.txtDate);
		txtDate.setText(Event.dateTimeFormat2.format(item.date));
		
		TextView txtAmount = (TextView)convertView.findViewById(R.id.txtAmount);
		txtAmount.setText(item.amount+"");
		
		ImageView imgType = (ImageView)convertView.findViewById(R.id.imgType);
		if (item.type == BalanceItem.TYPE_IN) {
			imgType.setImageResource(R.drawable.balance_ok);
		} else {
			imgType.setImageResource(R.drawable.balance_error);
		}
		
		return convertView;
	}
	
	public void setItems(ArrayList<BalanceItem> items) {
		clear();
		this.items = items;
		for(BalanceItem item : items) {
			add(item);
		}
		notifyDataSetChanged();
	}

}
